﻿using System.Linq;
using VNet.VisiSuite.VisiBoard.Common.Helpers;

namespace VNet.VisiSuite.VisiBoard.Models
{
    public abstract class ComparableBaseModel<T> : BaseModel
    {
        private bool _isChange;
        public bool IsChange
        {
            get { return _isChange; }
            set
            {
                _isChange = value;
                OnPropertyChanged("IsChange");
            }
        }

        private bool _isCancelEnable;
        public bool IsCancelEnable
        {
            get { return _isCancelEnable; }
            set
            {
                _isCancelEnable = value;
                OnPropertyChanged("IsCancelEnable");
            }
        }

        [IgnoreClone]
        public T OriginalObject { get; set; }

        protected void OnPropertyChanged(string propertyName, bool mustCheckChange)
        {
            if (mustCheckChange)
                CheckChange();
            base.OnPropertyChanged(propertyName);
        }

        public bool HasChange()
        {
            return !Equals(OriginalObject);
        }

        public void CheckChange()
        {
            if (OriginalObject != null)
            {
                IsCancelEnable = IsChange = HasChange();
                if (IsChange) ErrorMessage = null;
            }
        }
    }
}